from train import paginations
from rest_framework.views import APIView
from utils.response import APIResponse
from . import search_data
from utils.logging import search

class Search_data(APIView):
    def get(self, request, *args, **kwargs):
        data=request.query_params.get('key')
        search.info('用户搜索了%s'%data)
        page=int(request.query_params.get('page'))-1
        page_size=int(request.query_params.get('page_size'))
        try:
            data_list=search_data.data_search(data,page,page_size)
            print(data_list)
            count=data_list["hits"]['total']['value']
            data_list=data_list["hits"]["hits"]
            data_set = []
            for data_base in data_list:
                data_base=data_base['_source']['results']
                data_set.append(data_base)
            return APIResponse(results=data_set,count=count)
        except Exception as e:
            print(e)
